package com.sushang.controller;

import com.sushang.pojo.Article;
import com.sushang.pojo.PageBean;
import com.sushang.pojo.Result;
import com.sushang.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author chengzhouli
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleServic;

    /***
     * 添加文章
     * */

    @PostMapping
    public Result add(@RequestBody @Validated Article article) {
        articleServic.add(article);
        return Result.success();
    }

    /**
     * 文章列表(条件分页)
     **/
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ) {
        PageBean<Article> pb = articleServic.list(pageNum, pageSize, categoryId, state);
        return Result.success(pb);
    }

    /***
     *
     *获取文章详情
     *
     * **/
    @GetMapping("/detail")
    public Result<Article> detail(Integer id) {
        Article article = articleServic.findById(id);
        return Result.success(article);
    }
    /***
     *
     *更新文章
     *
     * **/
    @PutMapping
    public Result update(@RequestBody @Validated Article article){
        articleServic.update(article);
        return Result.success();
    }
    /**
     * 删除文章
     * **/
    @DeleteMapping
    public Result delete(Integer id){
        articleServic.deleteById(id);
        return Result.success();
    }
}
